package com.ease.dao.ibatis.typehandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.ibatis.sqlmap.engine.type.BaseTypeHandler;
import com.ibatis.sqlmap.engine.type.TypeHandler;

/**
 * 
 * 
 * @author nathanleewei
 * 
 */
@Deprecated
/* use com.ibatis.sqlmap.engine.type.BooleanTypeHandler instead */
public class BooleanTypeHandler extends BaseTypeHandler implements TypeHandler {

	private static final byte TRUE = 1;

	private static final byte FALSE = 0;

	private Object getResult(Object object) throws SQLException {
		Byte value = (Byte) object;
		if (null != value) {
			if (TRUE == value.byteValue()) {
				return new Boolean(true);
			} else {
				return new Boolean(false);
			}
		} else {
			return null;
		}
	}

	public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
		return getResult(cs.getObject(columnIndex));
	}

	public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
		return getResult(rs.getObject(columnIndex));
	}

	public Object getResult(ResultSet rs, String columnName) throws SQLException {
		return getResult(rs.getObject(columnName));
	}

	public void setParameter(PreparedStatement ps, int i, Object parameter, String jdbcType) throws SQLException {
		if (null != parameter) {
			if (((Boolean) parameter).booleanValue()) {
				ps.setByte(i, TRUE);
			} else {
				ps.setByte(i, FALSE);
			}
		} else {
			ps.setNull(i, oracle.jdbc.OracleTypes.NUMBER);
		}
	}

	public Object valueOf(String value) {
		if (null != value) {
			if (Boolean.valueOf(value).booleanValue()) {
				return Boolean.TRUE;
			} else {
				return Boolean.FALSE;
			}
		}
		return null;
	}

}
